﻿function Widget_YoutubeBrowser(obj)
{
   this.initializeBase(obj);
}
Widget_YoutubeBrowser.prototype = new WidgetInstance;

Widget_YoutubeBrowser.prototype.Save = function()
{
   if (myTabs.hasRequiredTabPermission(this.Tab, TabAccessLevel.Widget_Edit))
   {
      JSON.Widget_UpdateProperty(this.Widget.Tab.id, this.id, "state", this.Widget.obj.State);
      this.Refresh();
   }
}

Widget_YoutubeBrowser.prototype.Edit = function()
{
   this.Widget.InstanceEdit();
   if (myTabs.hasRequiredTabPermission(this.Tab, TabAccessLevel.Widget_Edit))
   {
      if(this.inEditMode)
         this.Settings_TxtCountryCode.focus();
   }   
}

Widget_YoutubeBrowser.prototype.Refresh = function()
{
   if (this.Settings_txtSearch.value.length > 0)
   {
      this.FeedURL = "http://gdata.youtube.com/feeds/api/videos?alt=rss&orderby=published&max-results=50&vq=" + this.Settings_txtSearch.value;
      Proxy.GetRssFeed(this.FeedURL,0,10,false,Function.createDelegate( this, this.onContentLoad ));
   }
   else
   {
      this.WidgetDiv_BodyContent.innerHTML = "No search term defined";
   }
}

Widget_YoutubeBrowser.prototype.onContentLoad = function(rss)
{
   WriteHistory("Widget_YoutubeBrowser.prototype.onContentLoad #" + this.id);
     var div = this.WidgetDiv_BodyContent;
     div.innerHTML = "";
     
     if (!rss)
     {
        div.innerHTML = "we are unable to grab the rss file";
        return;
     }
     else if (rss.length < 1)
     {
      div.innerHTML = "No news was returned";
     }

     // creates a <table> element and a <tbody> element
     
     //table element
     var tbl     = document.createElement("table");
     tbl.className="rss_tbl";
     
     //table body element
     var tblBody = document.createElement("tbody");
     
     //replace string for regular expression
     var replacestr = "\\'";

     //gets the element width
     var eWidth = getElementWidth(div);
     
     //assuming each character has a max width of 5
     var HeaderDesc = eWidth / 5;
     var BulletDesc = HeaderDesc - 3;
     
     var tmp = "";
     
     for( var i = 0; i < rss.length; i ++ )
     {
         var item = rss[i];
         
         //2nd onwards create as bullets
         if (item.Title.length > BulletDesc)
         {
            item.Title = item.Title.substring(0, BulletDesc) + "..";
         }
         
         if (item.ContentEncoded.length > 0)
         {
            //if there is Content, use the content, else use the description
            item.ODesc = "";
            item.Description = "";
         }
         else
         {
            item.ODesc = item.Description.replace(/'/g, replacestr).replace(/"/g, replacestr);
         }            

         var row = document.createElement("tr");
         row.setAttribute("valign", "top");
         var ElementID = this.id + "_" + i;

         var cell2 = document.createElement("td");

         cell2.innerHTML = "<a class='feed_item_link'" +
               (item.Link.length > 0 ? " target='_blank' href='" + item.Link + "'" : " href='javascript:void(0)'") +
               "><span style='text-decoration:underline'>" + item.Title + 
               "</span><div id='wrss_m" + ElementID + "'>" +
               (item.ContentEncoded.length < 1 ? "<span class='feed_item_link'>" + item.Description + "</span>": item.ContentEncoded) +
               "</div></a><hr>";
         
         row.appendChild(cell2);
         tblBody.appendChild(row);
     }
     tbl.appendChild(tblBody);
     div.appendChild(tbl);   
}

Widget_YoutubeBrowser.prototype.Load = function()
{
   this.Menu_Edit = new Array();
   this.Menu_Edit.push("m" + this.id + "_edit");
   this.Widget.InstanceLoad();
      
   this.WidgetDiv_BodySettings = $get('wdivbs' + this.id);
   this.WidgetDiv_BodyContent = document.getElementById("wdivbc" + this.Widget.id);
   this.WidgetDiv_Command_Edit = document.getElementById("wdivc_edit" + this.Widget.id);
   
   this.Settings_txtSearch = $get('txtSearch' + this.id);
   this.Settings_txtSearch.value = this.Widget.obj.State;
   this.Refresh();  
   
  this.Widget.InstanceLoadComplete();
}

Widget_YoutubeBrowser.prototype.Save = function()
{
   this.Widget.obj.State = this.Settings_txtSearch.value;
   
   //Save only if have permission, else ignore
   if (myTabs.hasRequiredTabPermission(this.Tab, TabAccessLevel.Widget_Edit))
      JSON.Widget_UpdateProperty(this.Widget.Tab.id, this.id, "state", this.Widget.obj.State);
      
   this.Refresh();   
}

Widget_YoutubeBrowser.prototype.SearchKeyPress = function(e)
{
   var keynum;

   if(window.event) // IE
     keynum = e.keyCode;
   else if(e.which) // Netscape/Firefox/Opera
     keynum = e.which;
     
  if (keynum == 13)
  {
     this.Save();
     return false;
  }
}